package com.bdi.mqtt_broker.handler;

import com.alibaba.fastjson.JSONObject;
import com.bdi.mqtt_broker.model.InternalMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * @author WangNing yogehaoren@gmial.com <br>
 * @since 1.0
 */
@Slf4j
public class MessageHandler extends SimpleChannelInboundHandler<InternalMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, InternalMessage msg) throws Exception {


        log.debug("Get Message: {}", JSONObject.toJSONString(msg));

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("ExceptionCaught with {}", cause.getMessage());
        cause.printStackTrace();
        ctx.close();
    }
}
